<template>
  <div>
    <div class="ef-node-form">
      <div class="ef-node-form-header">编辑</div>
      <div class="ef-node-form-body">
        <el-form
          :model="node"
          ref="dataForm"
          :rules="rules"
          label-width="80px"
          v-show="type === 'node'"
        >
          <el-form-item label="类型" prop="type">
            <el-input v-model="node.type" :disabled="true"></el-input>
          </el-form-item>
          <el-form-item label="名称" prop="name">
            <el-input v-model="node.name"></el-input>
          </el-form-item>
          <!-- <el-form-item label="ico图标">
            <el-input v-model="node.ico"></el-input>
          </el-form-item> -->
          <!-- <el-form-item label="配置信息">
            <el-input type="textarea" v-model="node.content"></el-input>
          </el-form-item> -->
          <!-- 选择点位 -->
          <div v-if="node.type == 'deviceDataSource'">
            <el-row :gutter="5">
              <el-col :span="8">
                <el-select v-model="node.deviceParamsLeft1">
                  <el-option label="变量" value="变量"> </el-option>
                  <el-option label="参数" value="参数"> </el-option>
                </el-select>
              </el-col>
              <el-col :span="16">
                <ElInputDevSelect
                  id="deviceParamsLeft1Id"
                  label="deviceParamsLeft1Name"
                  type="dev"
                  v-model="node"
                  :multiple="true"
                  v-if="node.deviceParamsLeft1 === '参数'"
                ></ElInputDevSelect>
                <el-input v-else v-model="node.deviceParamsLeft2"></el-input>
              </el-col>
            </el-row>
            <el-select v-model="node.deviceParamsOperate">
              <el-option label="加" value="加"> </el-option>
              <el-option label="减" value="减"> </el-option>
              <el-option label="乘" value="乘"> </el-option>
              <el-option label="除" value="除"> </el-option>
              <el-option label="大于" value="大于"> </el-option>
              <el-option label="小于" value="小于"> </el-option>
            </el-select>
            <el-row :gutter="5">
              <el-col :span="8">
                <el-select v-model="node.deviceParamsRight1">
                  <el-option label="变量" value="变量"> </el-option>
                  <el-option label="参数" value="参数"> </el-option>
                </el-select>
              </el-col>
              <el-col :span="16">
                <ElInputDevSelect
                  id="deviceParamsRight1Id"
                  label="deviceParamsRight1Name"
                  type="dev"
                  v-model="node"
                  :multiple="true"
                  v-if="node.deviceParamsRight1 === '参数'"
                ></ElInputDevSelect>
                <el-input v-else v-model="node.deviceParamsRight2"></el-input>
              </el-col>
            </el-row>
          </div>

          <!-- 通知提醒 -->
          <el-form-item label="上升趋势时长" v-if="node.type == 'taskUp'">
            <el-input v-model="node.duration"></el-input>
          </el-form-item>
          <el-form-item label="下降趋势时长" v-if="node.type == 'taskDown'">
            <el-input v-model="node.duration"></el-input>
          </el-form-item>

          <!-- 报警类型 -->

          <el-form-item label="消息模板" v-if="node.type == 'endAlert'">
            <el-input
              type="textarea"
              :autosize="{ minRows: 2, maxRows: 4 }"
              v-model="node.alertMessage"
            ></el-input>
          </el-form-item>
          <span v-if="node.type == 'endAlert'" class="alertMessage"
            >消息模板格式例如：<br />
            设备${this.device.deviceName}【${this.device.serialNumber}】在${this.data.time}发生报警，请注意处理！
            说明：1.[this.device]为设备信息，${this.data.time}
            为报警时间。</span
          >
          <!-- 通知提醒 -->
          <el-form-item label="通知名称" v-if="node.type == 'endNotice'">
            <el-input v-model="node.noticeName"></el-input>
          </el-form-item>
          <el-form-item label="通知人员" v-if="node.type == 'endNotice'">
            <!-- <el-input type="textarea" v-model="node.noticePeople"></el-input> -->
            <ElInputSelect
              id="peronId"
              label="personName"
              v-model="node"
              :multiple="true"
            ></ElInputSelect>
          </el-form-item>
          <!--
          <el-form-item label="状态">
            <el-select v-model="node.state" placeholder="请选择">
              <el-option
                v-for="item in stateList"
                :key="item.state"
                :label="item.label"
                :value="item.state"
              >
              </el-option>
            </el-select>
          </el-form-item> -->
          <el-form-item>
            <!-- <el-button icon="el-icon-close">重置</el-button> -->
            <el-button type="primary" icon="el-icon-check" @click="save"
              >保存</el-button
            >
          </el-form-item>
        </el-form>

        <el-form
          :model="line"
          ref="dataForm"
          label-width="80px"
          v-show="type === 'line'"
        >
          <el-form-item label="条件">
            <el-input v-model="line.label"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-close">重置</el-button>
            <el-button type="primary" icon="el-icon-check" @click="saveLine"
              >保存</el-button
            >
          </el-form-item>
        </el-form>
      </div>
      <div class="el-node-form-tag"></div>
    </div>
  </div>
</template>

<script>
import { cloneDeep } from 'lodash'

export default {
  data () {
    return {
      visible: true,
      // node 或 line
      type: 'node',
      node: {
        type: null,
        name: null
      },
      line: {},
      data: {},
      stateList: [
        {
          state: 'success',
          label: '成功'
        },
        {
          state: 'warning',
          label: '警告'
        },
        {
          state: 'error',
          label: '错误'
        },
        {
          state: 'running',
          label: '运行中'
        }
      ],
      rules: {
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }]
      }
    }
  },
  methods: {
    /**
     * 表单修改，这里可以根据传入的ID进行业务信息获取
     * @param data
     * @param id
     */
    nodeInit (data, id) {
      this.type = 'node'
      this.data = data
      data.nodeList.filter((node) => {
        if (node.id === id) {
          this.node = cloneDeep(node)
        }
      })
    },
    lineInit (line) {
      this.type = 'line'
      this.line = line
    },
    // 修改连线
    saveLine () {
      this.$refs.dataForm.validate((valid) => {
        if (valid) {
          this.$emit(
            'setLineLabel',
            this.line.from,
            this.line.to,
            this.line.label
          )
        }
      })
    },
    save () {
      this.$refs.dataForm.validate((valid) => {
        if (valid) {
          this.$message.success('当前节点信息保存成功')
          this.data.nodeList.filter((node) => {
            if (node.id === this.node.id) {
              node.name = this.node.name
              node.left = this.node.left
              node.top = this.node.top
              node.ico = this.node.ico
              node.state = this.node.state
              node.content = this.node.content
              // 选择点位
              node.deviceParamsLeft1 = this.node.deviceParamsLeft1
              node.deviceParamsLeft2 = this.node.deviceParamsLeft2
              node.deviceParamsLeft1Id = this.node.deviceParamsLeft1Id
              node.deviceParamsLeft1Name = this.node.deviceParamsLeft1Name

              node.deviceParamsOperate = this.node.deviceParamsOperate

              node.deviceParamsRight1 = this.node.deviceParamsRight1
              node.deviceParamsRight2 = this.node.deviceParamsRight2
              node.deviceParamsRight1Id = this.node.deviceParamsRight1Id
              node.deviceParamsRight1Name = this.node.deviceParamsRight1Name
              // 通知提醒
              node.duration = this.node.duration
              //   报警类型
              node.alertType = this.node.alertType
              node.alertLevel = this.node.alertLevel
              node.alertName = this.node.alertName
              node.alertStatus = this.node.alertStatus
              node.alertRemark = this.node.alertRemark
              node.alertMessage = this.node.alertMessage

              // 通知提醒
              node.noticeName = this.node.noticeName
              node.peronId = this.node.peronId
              node.personName = this.node.personName

              this.$emit('repaintEverything')
            }
          })
        }
      })
    }
  }
}
</script>

<style>
.el-node-form-tag {
  position: absolute;
  top: 50%;
  margin-left: -15px;
  height: 40px;
  width: 15px;
  background-color: #fbfbfb;
  border: 1px solid rgb(220, 227, 232);
  border-right: none;
  z-index: 0;
}

.alertMessage {
  color: red;
  font-size: 12px;
}
</style>
